package com.example.chapter3.homework.List;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chapter3.homework.R;
import java.util.List;

public class CodeLanAdapter extends RecyclerView.Adapter<CodeLanAdapter.ViewHolder> {
    private List<CodeLanguage> mCodeLanList;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View codeLanView;
        ImageView codeLanImage;
        TextView codeLanName;
        TextView codeLanRate;
        TextView codeLanOrder;

        public ViewHolder(View view){
            super(view);
            codeLanView=view;
            codeLanImage=(ImageView)view.findViewById(R.id.languageImage);
            codeLanName=(TextView)view.findViewById((R.id.languageName));
            codeLanRate=(TextView)view.findViewById(R.id.languageRate);
            codeLanOrder=(TextView)view.findViewById(R.id.languageOrder);
        }
    }

    public CodeLanAdapter(List<CodeLanguage> codeLanguages){
        mCodeLanList=codeLanguages;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_text_list,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.codeLanView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                CodeLanguage codeLanguage=mCodeLanList.get(position);
                Toast.makeText(view.getContext(),"You click "+codeLanguage.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.codeLanImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                CodeLanguage codeLanguage=mCodeLanList.get(position);
                Toast.makeText(view.getContext(),"You click image "+codeLanguage.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }


    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CodeLanguage codeLanguage=mCodeLanList.get(position);
        holder.codeLanImage.setImageResource(codeLanguage.getImageId());
        holder.codeLanName.setText(codeLanguage.getName());
        holder.codeLanRate.setText(codeLanguage.getRate());
        holder.codeLanOrder.setText(codeLanguage.getOrder());
    }

    @Override
    public int getItemCount() {
        return mCodeLanList.size();
    }
}
